/*
 * detect_seed.c
 *
 *  Created on: 2024年5月9日
 *      Author: longhappiness
 */
#include "zf_common_headfile.h"
#define HEIGHT 120
#define WIDTH 188

#define RESULT_WIDTH 128
#define RESULT_HEIGHT 110
//基础循迹
uint8_t height = RESULT_HEIGHT, width = RESULT_WIDTH;
uint8_t seed_left[RESULT_HEIGHT] = { 0 };
uint8_t seed_right[RESULT_HEIGHT] = { 0 };
uint8_t middle_line[RESULT_HEIGHT] = { 0 };
// uint8_t garage_out_flag = 0, garage_in_flag = 1;
uint8_t middle_line_end = 29;

//最长白列
uint8_t length[RESULT_WIDTH] = { 0 };
uint8_t longest = 0;
uint8_t _longest = 0;
uint8_t white_col = 0;

////丢线
//uint8_t zuodiuxian_cnt = 0; // ���м��ζ���
//uint8_t zuodiuxian_flag = 0;
//uint8_t zuodiuxian[HEIGHT] = {0};
//uint8_t zuodiuxian_start[HEIGHT] = {0};
//
//uint8_t youdiuxian_cnt = 0; // ���м��ζ���
//uint8_t youdiuxian_flag = 0;
//uint8_t youdiuxian[HEIGHT] = {0};
//uint8_t youdiuxian_start[HEIGHT] = {0};

//_height必须小于等于110，因为RESULT_HEIGHT = 110

void detect_seed(uint8_t _height) {

    //最长白列变量初始化很重要！！！
    white_col = 0;
    longest = 0;
    _longest = 0;
    //length[RESULT_WIDTH] = { 0 };
    minima_left[0] = 0; // 用于存储找到的两个负极值
    minima_left[1] = 0;
    indices_left[0] = 0; // 用于存储负极值的下标
    indices_left[1] = 0;
    foundFirstNonzero_left = false; // 标记是否从后到前找到了第一个不为零的数
    operator_count_left = 0;
    operator_count_flag_left = 0;

    // 右侧数据处理
    minima_right[0] = 0;
    minima_right[1] = 0;
    indices_right[0] = 0; // 用于存储负极值的下标
    indices_right[1] = 0;
    foundFirstNonzero_right = false; // 标记是否从后到前找到了第一个不为零的数
    operator_count_right = 0;
    operator_count_flag_right = 0;

    //RESULT_WIDTH=128
    for (uint8_t i = 40; i < 88; i++) {
        length[i] = 0; //初始化
        for (uint8_t j = RESULT_HEIGHT - 3; j > 1; j--) { //height 107
            if (PerImg_threshold(j, i) == 0) {
                length[i] = 0;
                continue;
            }
            if (PerImg_threshold(j, i) == 255) {
                length[i]++;
                if (PerImg_threshold(j - 1, i) == 0) {
                    break;
                }
            }

        }
    }

    for (uint8_t i = 40; i < 88; i++) { //与上一循环保持一致
        if (length[i] > longest) {
            longest = length[i];
            _longest = i;
        }
    }

    if (length[_longest] > 100 && length[_longest + 10] > 100) {
        _longest = _longest + 5;
        longest = length[_longest];
        if (length[_longest + 20] > 100) {
            _longest = _longest + 5;
            longest = length[_longest];
            if (length[_longest + 30] > 100) {
                _longest = _longest + 5;
                longest = length[_longest];
            }
        }
    }

    //循迹代码
    //变量初始化
    uint8_t x = 0;
    uint8_t y = 0;
    uint8_t last_col = 0;
    uint8_t left_cnt = 0;
    uint8_t right_cnt = 0;
    uint8_t end_height = 5;    //牢记
    for (y = _height; y > end_height; y--) {    //height 109 to 6
        if (y == _height) {
            x = (uint8_t) RESULT_WIDTH * 0.5;
        } else {
            x = last_col;
        }

        //利用最长白列进行校正
        if (longest > 90 && y < 80 && circle_in == 0) {    //
            x = _longest;
            //last_col = x;
        }

        for (left_cnt = x; left_cnt > 0; left_cnt--) {    //width 0 to x
            uint8_t left_delta = PerImg_threshold(y, left_cnt)
                    - PerImg_threshold(y, left_cnt - 1);

            if (left_delta > 200) {
                seed_left[y] = left_cnt;
                break;
            } else {
                seed_left[y] = 0;
            }
        }

        calculateOperatorResult(y, 1);

        for (right_cnt = x; right_cnt < width - 1; right_cnt++) { //width x to 127

            uint8_t right_delta = PerImg_threshold(y, right_cnt)
                    - PerImg_threshold(y, right_cnt + 1);

            if (right_delta > 200) {
                seed_right[y] = right_cnt;
                break;
            } else {
                seed_right[y] = width - 1;
            }
        }
        //拐点
        calculateOperatorResult(y, 2);

        //计算中点，会利用最长白列和圆环的状态进行修正
        x = (uint8_t) ((seed_left[y] + seed_right[y]) / 2);
        middle_line[y] = x;

        //利用十字纠正 不好用
//        if (crossroads_flag == 1 && longest > 100) {
//            x = _longest;
//            last_col = x;
//            middle_line[y] = x;
//        }

        last_col = middle_line[y];

        //circle_in = Attr2[24];
        if (circle_right_flag == 1 && circle_left_flag == 0) {
            if (circle_in == 1 || circle_in == 1) {
                if (y < 100) {
                    middle_line[y] =
                            seed_right[y] - 20 > 0 ? seed_right[y] - 20 : 0;

                    last_col = seed_right[y] - 20 > 0 ? seed_right[y] - 20 : 0;

                }
            }
        } else if (circle_right_flag == 0 && circle_left_flag == 1) {
            if (circle_in == 1 || circle_in == 1) {
                if (y < 100) {
                    middle_line[y] =seed_left[y] + 20 < 127 ? seed_left[y] + 20 : 127;
                    last_col = seed_left[y] + 20 < 127 ? seed_left[y] + 20 : 127;


                }
            }
        }

        //这句代码是为了在显示屏上显示
        //image_threshold[y][middle_line[y]] = 0;
        //PerImg_threshold(y, right_cnt)
        if (end_height < y && y < 50
                && (((PerImg_threshold(y - 1, middle_line[y]) == 0
                        || PerImg_threshold(y - 2, middle_line[y]) == 0)
                        && ((circle_in == 0
                                && ((longest > 90
                                        && PerImg_threshold(y - 1, _longest)
                                                == 0) || (longest <= 90)))
                                || circle_in == 1) //如果要利用最长白列，就要确保没有识别到circle_in
                ) || y == end_height + 3
                //如果最白长列够长，中线下一点只要不是黑的，那就可以不break
                )) {
            middle_line_end = y;
            //拐点
            right_tp_cnt = 0;
            left_tp_cnt = 0;
            calculateOperatorResult(1, 3);
//            escape_barrier();
            circle(105);

            if (circle_before == 0 && circle_in == 0 && circle_at == 0
//                    &&circle_left_flag == 0 &&
//                    circle_right_flag == 0
//                    && circle_out == 0
                    ) {
                conect_line_branch();
            }
            break;
        }
    }
}
